﻿@using NewLife.Common;
@using NewLife.Model;
@using XCode.Membership;
@{
    var user = ViewBag.User as IUser ?? User.Identity as IUser;

    var fact = ObjectContainer.Current.Resolve<IMenuFactory>();

    var Menus = fact.Root.Childs;
    if (user != null && user.Role != null)
    {
        Menus = fact.GetMySubMenus(fact.Root.ID, user);
    }

    // 如果顶级只有一层，并且至少有三级目录，则提升一级
    if (Menus.Count == 1 && Menus[0].Childs.All(m => m.Childs.Count > 0)) { Menus = Menus[0].Childs; }

    String[] icons = new String[] { "flaticon-cogwheel", "flaticon-squares-4", "flaticon-imac", "flaticon-responsive", "flaticon-cogwheel",
        "flaticon-interface-1", "flaticon-interface-3", "flaticon-web", "flaticon-interface-8", "flaticon-time-1", "flaticon-app",
        "flaticon-map-location","flaticon-grid-menu-v2","flaticon-paper-plane", "flaticon-network", "flaticon-squares-3",
        "flaticon-interface-9","flaticon-graphic-2","flaticon-layers","flaticon-line-graph","flaticon-clock-1", "flaticon-layers" };
    String[] svgIcons = new String[]
    {
        "Shopping/Box#2.svg", "General/Duplicate.svg", "General/Attachment#2.svg", "Shopping/Box#1.svg", "Shopping/Wallet.svg",
        "Design/Substract.svg", "Design/Layers.svg", "Design/Interselect.svg", "Home/Water-mixer.svg", "Home/Mirror.svg", "Home/Library.svg",
        "Home/Book-open.svg", "Home/Cupboard.svg", "Home/Commode#2.svg", "Home/Commode#1.svg", "Design/Line.svg", "Shopping/Barcode-read.svg",
        "Shopping/Chart-bar#1.svg", "General/Settings-1.svg", "Communication/Send.svg", "Communication/Group-chat.svg", "Map/Direction#2.svg",
        "Layout/Layout-right-panel-1.svg", "Layout/Layout-top-panel-4.svg", "Layout/Layout-4-blocks.svg", "Layout/Layout-3d.svg",
        "Layout/Layout-top-panel-2.svg", "Layout/Layout-horizontal.svg", "Layout/Layout-arrange.svg", "Devices/Diagnostics.svg",
        "Tools/Pantone.svg", "Code/Git#2.svg", "Code/Compiling.svg"
            };

    Int32 _idx = 0;

    var cfg = ViewBag.Config as SysConfig;
    var name = cfg == null ? "" : cfg.DisplayName;

    var _area = ViewContext.RouteData.DataTokens["area"] + "";
}

<!--侧边栏菜单-->
<div class="kt-aside-menu-wrapper kt-grid__item kt-grid__item--fluid" id="kt_aside_menu_wrapper">
    <div id="kt_aside_menu" class="kt-aside-menu " data-ktmenu-vertical="1" data-ktmenu-scroll="1" data-ktmenu-dropdown-timeout="500">
        <ul class="kt-menu__nav">
            <!-- 循环加载菜单项 -->
            @foreach (var menu in Menus.Where(m => m.Visible))
            {
                var childs = fact.GetMySubMenus(menu.ID, user).Where(m => m.Visible);
                if (_idx >= icons.Length)
                {
                    _idx = 0;
                }

                if (childs.Any())
                {
                    var _open = _area == menu.Name ? "kt-menu__item--open" : "";
                    <li class="kt-menu__item kt-menu__item--submenu @_open" aria-haspopup="true" data-ktmenu-submenu-toggle="hover">
                        <a href="javascript:;" class="kt-menu__link kt-menu__toggle">
                            @if (!menu.Icon.IsNullOrEmpty())
                            {
                                <i class="kt-menu__link-icon @menu.Icon" style="font-size:20px;"></i>
                            }
                            else
                            {
                                <i class="kt-menu__link-icon @icons[_idx++]" style="font-size:20px;"></i>
                            }
                            <span class="kt-menu__link-text  pl-2" style="font-size:16px;">@menu.DisplayName</span>
                            <i class="kt-menu__ver-arrow la la-angle-right"></i>
                        </a>
                        <div class="kt-menu__submenu">
                            <span class="kt-menu__arrow"></span>
                            <ul class="kt-menu__subnav">
                                <li class="kt-menu__item kt-menu__item--parent" aria-haspopup="true">
                                    <span class="kt-menu__link"><span class="kt-menu__link-text">@menu.DisplayName</span></span>
                                </li>
                                @foreach (var subMenu in childs)
                                {
                                    @Html.Partial("_Nav_AsideMenu_Item", subMenu)
                                }
                            </ul>
                        </div>
                    </li>
                }
                else
                {
                    var url = menu.Url.IsNullOrEmpty() ? "" : Url.Content(menu.Url);

                    <li class="kt-menu__item" aria-haspopup="true">
                        <a href="@url" class="kt-menu__link" target="_self">
                            @if (!@menu.Icon.IsNullOrEmpty())
                            {
                                <i class="kt-menu__link-icon @menu.Icon" style="font-size:20px;"></i>
                            }
                            else
                            {
                                <i class="kt-menu__link-icon @icons[_idx++]" style="font-size:20px;"><span></span></i>
                            }
                            <span class="kt-menu__link-text  pl-2" style="font-size:16px;">@menu.DisplayName</span>
                        </a>
                    </li>
                }
            }
        </ul>
    </div>
</div>